import { isDev } from "../utils/env";

const testToken =
  "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJxbWcuY24iLCJhdWQiOiJxbWcuY24iLCJpYXQiOjE3MzY1NjU2MDEsIm5iZiI6MTczNjU2NTY2MSwiZXhwIjoxNzM2NjM3NjAxLCJleHRlbmQiOnsiaWQiOjksIm5pY2tuYW1lIjoiXHU2YjIyXHU1MTUyIn19.LeCO5ph9mXm9loB1IvGvgzYvuGHIaWlqekSFBItdP5k";

const PREFIX = "__MGDJ__";

const APPKEY = {
  userInfo: PREFIX + "userInfo",
  token: PREFIX + "token",
  refreshToken: PREFIX + "refreshToken",
  invite: PREFIX + "invite",
  store: PREFIX + "store",
  real: PREFIX + "real",
  location: PREFIX + "location",
  route: PREFIX + "route",
  realToken: PREFIX + "realToken",
  serviceShare: PREFIX + "serviceShare",
  lnglat: PREFIX + "lnglat",
};

/**
 * 缓存用户token
 */
export const useSetTokenStorage = (data) => {
  let token = data;
  if (isDev) {
    // token = testToken;
  }
  uni.setStorageSync(APPKEY.token, token);
};

/**
 * 获取用户token
 */
export const useGetTokenStorage = () => {
  return uni.getStorageSync(APPKEY.token);
};

/**
 * 缓存刷新refresh_token
 */
export const useSetRefreshTokenStorage = (data) => {
  uni.setStorageSync(APPKEY.refreshToken, data);
};

/**
 * 获取refresh_token
 */
export const useGetRefreshTokenStorage = () => {
  return uni.getStorageSync(APPKEY.refreshToken);
};

/**
 * 缓存用户实名状态
 */
export const useSetRealStorage = (data) => {
  uni.setStorageSync(APPKEY.real, data);
};

/**
 * 获取用户实名状态
 */
export const useGetRealStorage = () => {
  return uni.getStorageSync(APPKEY.real);
};

/**
 * 缓存用户信息
 */
export const useSetUserStorage = (data) => {
  if (!data) return;
  // 生产环境
  uni.setStorageSync(APPKEY.userInfo, data);
};
/**
 * 获取用户信息
 */
export const useGetUserStorage = () => {
  const userInfo = uni.getStorageSync(APPKEY.userInfo);
  const len = Object.keys(userInfo).length;
  if (len > 0) {
    return userInfo;
  }
  return {
    avatar: "/logo.png",
    nickname: "未登录",
  };
};

/**
 * 缓存邀请人id
 */
export const useSetInviteIdStorage = (data) => {
  uni.setStorageSync(APPKEY.invite, data);
};

/**
 * 获取邀请人id
 */
export const useGetInviteIdStorage = () => {
  return uni.getStorageSync(APPKEY.invite);
};

/**
 * 清除邀请人id缓存
 */
export const useRemoveInviteIdStorage = () => {
  uni.removeStorageSync(APPKEY.invite);
};

/**
 * 缓存用户选择的地市信息
 */
export const useSetLocationStorage = (data) => {
  uni.setStorageSync(APPKEY.location, data);
};

/**
 * 获取用户选择的地市信息
 */
export const useGetLocationStorage = () => {
  return uni.getStorageSync(APPKEY.location);
};

/**
 * 清除用户选择的地市信息
 */
export const useRemoveLocationStorage = () => {
  uni.removeStorageSync(APPKEY.location);
};

/**
 * 缓存当前页面路径信息
 * pages/index/index
 */
export const useSetCurrentRouteStorage = () => {
  const pages = getCurrentPages();
  if (pages.length > 0) {
    const curPage = pages[pages.length - 1];
    console.log("缓存当前页面路径信息", curPage.route);
    uni.setStorageSync(APPKEY.route, curPage.route);
  } else uni.setStorageSync(APPKEY.route, "");
};

/**
 * 获取当前页面路径信息
 * pages/index/index
 */
export const useGetCurrentRouteStorage = () => {
  return uni.getStorageSync(APPKEY.route);
};

/**
 * 清除当前页面路径信息
 */
export const useRemoveCurrentRouteStorage = () => {
  uni.removeStorageSync(APPKEY.route);
};

/**
 * 缓存实名认证token信息
 */
export const useSetRealTokenStorage = (data) => {
  uni.setStorageSync(APPKEY.realToken, data);
};
/**
 * 获取实名认证token信息
 */
export const useGetRealTokenStorage = () => {
  return uni.getStorageSync(APPKEY.realToken);
};
/**
 * 清除实名认证token信息
 */
export const useRemoveRealTokenStorage = () => {
  uni.removeStorageSync(APPKEY.realToken);
};

/**
 * 缓存日常服务分享信息(推荐官)
 * domesticServiceId 服务id
 * userId 用户id(推荐官id)
 */
export const useSetServiceShareStorage = (data) => {
  uni.setStorageSync(APPKEY.serviceShare, data);
};
/**
 * 获取日常服务分享信息(推荐官)
 * domesticServiceId 服务id
 * userId 用户id(推荐官id)
 */
export const useGetServiceShareStorage = () => {
  return uni.getStorageSync(APPKEY.serviceShare);
};
/**
 * 清除日常服务分享信息(推荐官)
 * domesticServiceId 服务id
 * userId 用户id(推荐官id)
 */
export const useRemoveServiceShareStorage = () => {
  uni.removeStorageSync(APPKEY.serviceShare);
};

/**
 * 缓存默认经纬度
 */
export const useSetLnglatStorage = (data) => {
  uni.setStorageSync(APPKEY.lnglat, data);
};
/**
 * 获取默认经纬度
 */
export const useGetLnglatStorage = () => {
  return uni.getStorageSync(APPKEY.lnglat);
};
/**
 * 清除默认经纬度
 */
export const useRemoveLnglatStorage = () => {
  uni.removeStorageSync(APPKEY.lnglat);
};
